<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceViewReport extends JView
{
	function display($tpl=null) {

		global $mainframe;

		//Initialize variables
		$document	= & JFactory::getDocument();
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$params 	= & $mainframe->getParams('com_ownservice');
		

		if($user->gid < 25) {
			$canView		= Ownservice::checkAccess('report', 'view', 'report', 'all');
			if ( !$canView ) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect( 'index.php?option=com_ownservice');
			}
		}

		if($this->getLayout() == 'transferredrecord') {
			$this->_displayTransferredRecord($tpl);
			return;
		}

		if($this->getLayout() == 'user') {
			$this->_displayUser($tpl);
			return;
		}

		if($this->getLayout() == 'transferreduser') {
			$this->_displayTransferredUser($tpl);
			return;
		}

		if($this->getLayout() == 'stafftransfer') {
			$this->_displayStaffTransfer($tpl);
			return;
		}

		if($this->getLayout() == 'transferredstafftransfer') {
			$this->_displayTransferredStaffTransfer($tpl);
			return;
		}

		if($this->getLayout() == 'installusrs') {
			$this->_displayInstallusrs($tpl);
			return;
		}

		JHTML::_('behavior.mootools');

		//add css to document
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/nav.css');
		//add java script to document
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//Get data form the model
		$model		= $this->getModel();
		$level		= & $ownuser->get('level_id');

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_TRANSFERRED_RECORD_ALL'));
		$document->setTitle( $params->get('page_title') );

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);

		parent::display($tpl);
	}

	function _displayTransferredRecord($tpl) {

		global $mainframe, $option;

		//Initialize variables
		$document	= & JFactory::getDocument();
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$params 	= & $mainframe->getParams('com_ownservice');

		$filter_order	= $mainframe->getUserStateFromRequest($option.'.transferredrecord.filter_order', 'filter_order', 't.requested_date', 'cmd');
		$filter_order_Dir= $mainframe->getUserStateFromRequest($option.'.transferredrecord.filter_order_Dir', 'filter_order_Dir', 'DESC', 'cmd');

		//Get vars
		$post = JRequest::get('Post');

		JHTML::_('behavior.mootools');

		//add css to document
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/nav.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/icon.css');
		//add java script to document
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//Get data form the model
		$model	= $this->getModel();
		$rows	= $this->get('Transferred');

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canDownload	= ($user->gid < 25)? Ownservice::checkAccess('report','download','report','all'):1;

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_TRANSFERRED_RECORD_ALL'));
		$document->setTitle( $params->get('page_title') );

		$lists = array();
		$lists['order']     = $filter_order;
		$lists['order_Dir'] = $filter_order_Dir;

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('access', $access);
		$this->assignRef('rows', $rows);
		$this->assignRef('data', $post);
		$this->assignRef('lists', $lists);

		parent::display($tpl);
	}

	function _displayUser($tpl) {

		global $mainframe;

		//Initialize variables
		$document	= & JFactory::getDocument();
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$params 	= & $mainframe->getParams('com_ownservice');

		JHTML::_('behavior.mootools');

		//add css to document
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/nav.css');
		
		//add java script to document
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');
		
		//add js autocomplete
		$document->addScript('components/com_ownservice/assets/js/jquery-1.7.2.min.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-autocomplete/jquery.bgiframe.min.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-autocomplete/jquery.ajaxQueue.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-autocomplete/jquery.autocomplete.min.js');
		//add css autocomplete
		$document->addStyleSheet('components/com_ownservice/assets/js/jquery-autocomplete/jquery.autocomplete.css');

		//ui
		$document->addStyleSheet('components/com_ownservice/assets/js/jquery-ui-1.9.1/themes/base/jquery.ui.all.css');
		//$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/jquery-1.8.2.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/external/jquery.bgiframe-2.1.2.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.core.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.widget.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.mouse.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.draggable.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.position.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.resizable.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.dialog.js');
		$document->addScriptDeclaration(
								'function selectUser(cid, membername)
								{
									document.adminForm.membername.value=membername;
									document.adminForm.user_id.value=cid;
									document.getElementById(\'sbox-window\').close();
								}
								
								jQuery(document).ready(function () {
									jQuery("#membername").autocomplete("index.php?option=com_ownservice&controller=report&task=viewmembers&format=raw&'.JUtility::getToken().'=1", {
									width: 260,
									max: 100,
									matchContains: false,
									mustMatch: false,
									selectFirst: false,
									dataType: "json",
									parse: function(data) {
										return jQuery.map(data, function(row) {
											return {
												data: row,
												value: row.name,
												result: row.name
											};
										});
									},
									formatItem: function(row) {
										return row.name;
									}
									}).result(function(e, row) {
										jQuery("#user_id").val(row.id);
									})
								 });
								');

		//Get data form the model
		$model		= $this->getModel();
		$level		= & $ownuser->get('level_id');
		//$users		= & $this->get('Users');

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_TRANSFERRED_RECORD_USER'));
		$document->setTitle( $params->get('page_title') );

		$lists = array();
		//$lists['user'] = OwnserviceHelper::buildusersselect($users, 'user_id','',true, 'class="inputbox"');

		JHTML::_('behavior.modal', 'a.modal');
		
		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('lists', $lists);

		parent::display($tpl);
	}

	function _displayTransferredUser($tpl) {

		global $mainframe;

		//Initialize variables
		$document	= & JFactory::getDocument();
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$params 	= & $mainframe->getParams('com_ownservice');

		//Get vars
		$user_id = JRequest::getVar('user_id', 0 , 'post', 'int');
		//Get vars
		$post = JRequest::get('Post');

		JHTML::_('behavior.mootools');

		//add css to document
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/nav.css');
		//$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/print.css', 'text/css', 'print');
		//add java script to document
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//Get data form the model
		$model		= $this->getModel();
		$rows		= $model->getTransferred($user_id);
		$level		= & $ownuser->get('level_id');
		$member		= $model->getUser($user_id);
		$accounts	= $model->getAccounts($user_id);

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canDownload	= ($user->gid < 25)? Ownservice::checkAccess('report','download','report','all'):1;

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_TRANSFERRED_RECORD_USER'));
		$document->setTitle( $params->get('page_title') );

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('access', $access);
		$this->assignRef('rows', $rows);
		$this->assignRef('accounts', $accounts);
		$this->assignRef('member', $member);
		$this->assignRef('data', $post);

		parent::display($tpl);
	}

	function _displayStaffTransfer($tpl) {

		global $mainframe;

		//Initialize variables
		$document	= & JFactory::getDocument();
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$params 	= & $mainframe->getParams('com_ownservice');

		JHTML::_('behavior.mootools');

		//add css to document
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/nav.css');
		//add java script to document
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//Get data form the model
		$model			= $this->getModel();
		$level			= & $ownuser->get('level_id');
		$stafftransfer	= & $this->get('StaffTransfer');
		
		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_TRANSFERRED_RECORD_STAFF_TRANSFER'));
		$document->setTitle( $params->get('page_title') );

		$lists = array();
		$lists['stafftransfer'] = OwnserviceHelper::buildusersselect($stafftransfer, 'transfer_id','',true, 'class="inputbox"');

		$this->assignRef('lists', $lists);

		parent::display($tpl);
	}

	function _displayTransferredStaffTransfer() {

		global $mainframe;

		//Initialize variables
		$document	= & JFactory::getDocument();
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$params 	= & $mainframe->getParams('com_ownservice');

		//Get vars
		$transferredid = JRequest::getVar('transfer_id', 0 , 'post', 'int');
		//Get vars
		$post = JRequest::get('Post');

		JHTML::_('behavior.mootools');

		//add css to document
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/nav.css');
		//add java script to document
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//Get data form the model
		$model		= $this->getModel();
		$rows		= $model->getStaffTransferred($transferredid);
		$level		= & $ownuser->get('level_id');

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canDownload	= ($user->gid < 25)? Ownservice::checkAccess('report','download','report','all'):1;

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_TRANSFERRED_RECORD_STAFF_TRANSFER'));
		$document->setTitle( $params->get('page_title') );

		$this->assignRef('access', $access);
		$this->assignRef('rows', $rows);
		$this->assignRef('data', $post);

		parent::display($tpl);
	}

	function _displayInstallusrs($tpl)
	{
		global $mainframe;

		//Initialize variables
		$document	= & JFactory::getDocument();
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$params 	= & $mainframe->getParams('com_ownservice');

		//add css to document
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'components/com_ownservice/assets/css/nav.css');
		//add java script to document
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//get data from model
		$model		= $this->getModel();
		$totalusers	= $this->get('TotalUsers');

		$lists = array();

		$this->assignRef('totalusers', $totalusers);

		parent::display($tpl);
	}
}